package com.study.studyroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.studyroom.entity.bo.RoomTypeBo;
import com.study.studyroom.entity.domain.RoomType;
import com.study.studyroom.entity.enums.RequestResultEnum;
import com.study.studyroom.entity.exception.GlobalException;
import com.study.studyroom.entity.vo.Condition;
import com.study.studyroom.entity.vo.room.RoomTypeVo;
import com.study.studyroom.mapper.RoomTypeMapper;
import com.study.studyroom.service.RoomTypeService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class RoomTypeServiceImpl extends BaseService implements RoomTypeService {

    private RoomTypeMapper roomTypeMapper;

    @Override
    public IPage<RoomTypeBo> getRoomType(Condition<RoomTypeVo> condition) {
        LambdaQueryWrapper<RoomType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomType::getDeleteFlag,false);
        if (condition.getQueryInfo()!=null){
            RoomTypeVo queryInfo = condition.getQueryInfo();
            if (StringUtils.isNotBlank(queryInfo.getTypeName())){
                lambdaQueryWrapper.like(RoomType::getTypeName,queryInfo.getTypeName());
            }
            if (queryInfo.getDataStatus()!=null){
                lambdaQueryWrapper.eq(RoomType::getDataStatus,queryInfo.getDataStatus());
            }
        }
        Page<RoomType> roomTypePage = roomTypeMapper.selectPage(Page.of(condition.getPageIndex(), condition.getPageSize()), lambdaQueryWrapper);
        Page<RoomTypeBo> result = new Page<>();
        result.setTotal(roomTypePage.getTotal());
        result.setRecords(new ArrayList<>());
        for (RoomType record : roomTypePage.getRecords()) {
            RoomTypeBo roomTypeBo = new RoomTypeBo();
            BeanUtils.copyProperties(record,roomTypeBo);
            result.getRecords().add(roomTypeBo);
        }
        return result;
    }

    @Override
    public Integer addRoomType(RoomTypeVo roomTypeVo) {
        if (roomTypeVo==null||
            StringUtils.isBlank(roomTypeVo.getTypeName())||
            roomTypeVo.getUnitType()==null||
            roomTypeVo.getStartPrice()==null||
            roomTypeVo.getUnitPrice()==null||
            roomTypeVo.getMaxPrice()==null){
            throw new GlobalException(RequestResultEnum.CUSTOM_PARAM_CHECK_FAILED);
        }
        RoomType roomType = new RoomType();
        BeanUtils.copyProperties(roomTypeVo,roomType);
        roomType.setIsDefault(false);
        roomType.setId((int) System.currentTimeMillis());
        return this.insert(roomTypeMapper, roomType);
    }

    @Override
    public RoomTypeBo updateRoomType(RoomTypeVo roomTypeVo) {
        RoomType newType = new RoomType();
        BeanUtils.copyProperties(roomTypeVo,newType);
        RoomType oldType = this.selectById(roomTypeMapper, roomTypeVo.getId());
        newType=this.update(roomTypeMapper, newType,oldType);
        RoomTypeBo roomTypeBo = new RoomTypeBo();
        BeanUtils.copyProperties(newType,roomTypeBo);
        return roomTypeBo;
    }

    @Override
    public RoomTypeBo updateRoomTypeStatus(Integer id) {
        RoomType roomType = this.selectById(roomTypeMapper,id);
        if (roomType.getIsDefault()){
            throw new GlobalException(RequestResultEnum.DISABLED_UPDATE_STATUS_AND_DELETE);
        }
        roomType=this.updateDataStatusById(roomTypeMapper,roomType);
        RoomTypeBo roomTypeBo = new RoomTypeBo();
        BeanUtils.copyProperties(roomType,roomTypeBo);
        return roomTypeBo;
    }

    @Override
    public Integer deleteRoomType(Integer id) {
        RoomType roomType = roomTypeMapper.selectById(id);
        if (roomType.getIsDefault()){
            throw new GlobalException(RequestResultEnum.DISABLED_UPDATE_STATUS_AND_DELETE);
        }
        return this.deleteById(roomTypeMapper,roomType);
    }

    @Override
    public List<RoomTypeBo> getAllRoomType() {
        LambdaQueryWrapper<RoomType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomType::getDeleteFlag,false)
                .eq(RoomType::getDataStatus,true)
                .orderByAsc(RoomType::getIsDefault);
        List<RoomType> roomTypes = roomTypeMapper.selectList(queryWrapper);
        List<RoomTypeBo> result = new ArrayList<>();
        for (RoomType roomType : roomTypes) {
            RoomTypeBo roomTypeBo = new RoomTypeBo();
            BeanUtils.copyProperties(roomType,roomTypeBo);
            result.add(roomTypeBo);
        }
        return result;
    }
}
